var app = new Vue({
  el: '#ppage',
  data() {
    return {
      BASE_PATH: BASE_PATH,
      title:"上传图片"
    };
  },
  mounted() {
    var id=getQueryVariable("id");
    if(id){
      this.title="编辑图片";
      _getDetail(this,id);
    }
  },
  methods: {
    showSites: function (alldatas) {
      this.sites = alldatas;
    },
    goupload: function () {
      window.location.href = "release_upload.html"
    }
  },
  updated() {

  }
});
var uploadPath = "";
function _getDetail(that,id){
  $.ajax({
    type: "post",
    url: BASE_PATH + "/app/icon/getIconById",
    dataType: "json",
    data: { id:id },
    success: function (data) {
      if (data.code == 1) {
        $("#iconid").val(data.data.id);
        $("#iconname").val(data.data.name);
        $("#iconurl").val(data.data.url);
        $("#iconsort").val(data.data.sort);
        $("#icontype").val(data.data.type);
        $("#demo1").attr("src", BASE_PATH + data.data.iconPath);
        uploadPath=data.data.iconPath;
      } else {
        layer.msg(data.msg)
      }
    },
  });
}
// _getFbFl(this);
layui.use(["croppers"], function () {
  var $ = layui.jquery,
    croppers = layui.croppers;
  //创建一个头像上传组件
  croppers.render({
    elem: "#test1",
    saveW: 400, //保存宽度
    saveH: 400, //保存高度
    mark: 1 / 1,
    area: "100vw", //弹窗宽度
    title: "上传图片",
    url: BASE_PATH + "/app/common/uploadpic", //图片上传接口返回和（layui 的upload 模块）返回的JOSN一样
    done: function (result) {
      console.log(result)
      //上传完毕回调
      if (result.code == 1) {
        $("#demo1").attr("src", BASE_PATH + result.data);
        uploadPath = result.data;
      } else {
        uploadPath = "";
        return layer.msg("上传失败");
      }
    },
    cancel: function () {
      layer.closeAll("page");
    },
  });
  $("#saveBtn").click(function () {
    var iconname = $("#iconname").val();
    var iconurl = $("#iconurl").val();
    var iconsort = $("#iconsort").val();
    var iconid=$("#iconid").val();
    if (iconname == "") {
      layer.msg("图标名不能为空");
      return false;
    }
    if (!isNumber(iconsort)) {
      layer.msg("排序号只能是数字")
      return false;
    }
    if (uploadPath=="") {
      layer.msg("文件没有选择")
      return false;
    }
    $.ajax({
      type: "post",
      url: BASE_PATH + "/app/icon/edit",
      dataType: "json",
      data: {id:iconid, name:iconname,url:iconurl,sort:iconsort,iconPath:uploadPath },
      success: function (data) {
        if (data.code == 1) {
          history.back(-1);
        } else {
          layer.msg(data.msg)
        }
      },
    });
  })
});

